package com.songyuquan.webcs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.songyuquan.webcs.pojo.Integrals;
import com.songyuquan.webcs.pojo.User;
import com.songyuquan.webcs.service.CartService;
import com.songyuquan.webcs.service.IntegralsService;
import com.songyuquan.webcs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CartService cartService;

    @Autowired
    private IntegralsService integralsService;

    @PostMapping("/register")
    public String register(User user, Model model, RedirectAttributes attributes) {
        user.setType(false);
        if (user.getGender() == 0) {
            user.setFileName("头像女孩.png");
        } else {
            user.setFileName("头像男孩.png");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_name", user.getLoginName());
        User loginUser = userService.getOne(wrapper);

        if (loginUser != null) {
            attributes.addFlashAttribute("error", "用户名已存在");
            return "redirect:/register";
        } else {
            userService.save(user);
            Integer id = user.getId();
            Integrals integrals = new Integrals();
            integrals.setUserId(id);
            integrals.setIntegral(0);
            integralsService.save(integrals);
            return "login";
        }
    }


    //登录
    @PostMapping("/login")
    public String login(String loginName, String password, HttpSession session) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_name", loginName);
        wrapper.eq("password", password);
        User user = userService.getOne(wrapper);
        if (user == null) {
            session.setAttribute("userError", "账户密码错误");
            return "login";
        } else {
            user.setPassword("0");
            session.setAttribute("user", user);
            return "redirect:/";
        }
    }


    //注销
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }

    //修改页面
    @GetMapping("/userUpdatePage")
    public ModelAndView getUserUpdatePage(HttpSession session) {
        User user = (User) session.getAttribute("user");
        User serviceById = userService.getById(user.getId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.addObject("userInfo", serviceById);
        modelAndView.setViewName("userUpdate");
        return modelAndView;
    }

    //修改
    @PostMapping("/userInfoUpdate")
    public String setUserInfo(
            @RequestParam("loginName") String loginName,
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            @RequestParam("gender") Integer gender,
            @RequestParam("identityCode") String identityCode,
            @RequestParam("email") String email,
            @RequestParam("mobile") String mobile,
            HttpSession session
    ) {

        User user = (User) session.getAttribute("user");

        User userUpdateVo = userService.getById(user.getId());


        if (!loginName.isEmpty()) {
            userUpdateVo.setLoginName(loginName);
        }

        if (!userName.isEmpty()) {

            userUpdateVo.setUserName(userName);
        }

        if (!password.isEmpty()) {
            userUpdateVo.setPassword(password);
        }

        userUpdateVo.setGender(gender);

        if (!identityCode.isEmpty()) {
            userUpdateVo.setIdentityCode(identityCode);
        }

        if (!email.isEmpty()) {
            userUpdateVo.setEmail(email);
        }

        if (!mobile.isEmpty()) {
            userUpdateVo.setMobile(mobile);
        }


        userService.updateById(userUpdateVo);
        session.setAttribute("user", userUpdateVo);

        return "redirect:/userInfo";
    }
}

